import { Condition } from './../../domain/models/condition';
import { ConditionService } from './../../service/conditionService';
import { ServiceManager } from './../../common/serviceFactory/serviceManager';
import * as Koa from 'koa';
import { route, RouteOptionsVerify, UsingLock } from "../../common";
import { ServiceException } from '../../common/excpetion';
const cSvr = ServiceManager.Current.GetService<ConditionService>('ConditionService');
class ConditionController {
    @route(new RouteOptionsVerify('GET', '/api/condition'))
    async queryConditions(ctx: Koa.Context, next) {
        ctx.response.body = null;
        await next();
    }
    @route(new RouteOptionsVerify('GET', '/api/condition/:id'))
    async getConditionInfoById(ctx: Koa.Context, next) {
        ctx.response.body = null;
        await next();
    }
    @route(new RouteOptionsVerify('POST', '/api/condition'))
    async createCondition(ctx: Koa.Context, next) {
        let data: Condition = ctx.request.body;
        let orgId = ctx.headers['orgid'];
        if (!data.name) {
            throw new ServiceException('请添加条件名称！');
        }
        await UsingLock(data.id, async () => {
            data.orgId = orgId;
            return cSvr.CreateCondition(data);
        });
        ctx.response.body = data;
        await next();
    }
    @route(new RouteOptionsVerify('PUT', '/api/condition'))
    async updateCondition(ctx: Koa.Context, next) {
        let data: Condition = ctx.request.body;
        let orgId = ctx.headers['orgid'];
        if (!data.name) {
            throw new ServiceException('请添加条件名称！');
        }
        await UsingLock(data.id, async () => {
            data.orgId = orgId;
            return cSvr.UpdateCondition(data);
        });
        ctx.response.body = data;
        await next();
    }
    @route(new RouteOptionsVerify('DELETE', '/api/condition/:id'))
    async deleteCondition(ctx: Koa.Context, next) {
        let { id } = ctx.params;
        await UsingLock(id, async () => {
            await cSvr.DeleteCondition(id);
        })
        ctx.response.body = {};
        await next();
    }
}